package com.nnnu.wsnackshop.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nnnu.wsnackshop.pojo.entity.Roles;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Set;

/**
 * <p>
 * 系统角色表 Mapper 接口
 * </p>
 *
 * @author zk
 * @since 2025-05-14
 */
@Mapper
public interface RolesMapper extends BaseMapper<Roles> {

    @Select("SELECT r.name FROM roles r"
            + " JOIN user_roles ur ON r.id=ur.role_id"
            + " JOIN users u ON ur.user_id=u.id"
            + " WHERE u.username=#{username}")
    Set<String> selectRoleNamesByUsername(@Param("username") String username);

    /**
     * 根据用户 ID 查询该用户的角色名称集合
     */
    @Select("""
      SELECT r.name
        FROM roles r
        JOIN user_roles ur ON r.id = ur.role_id
       WHERE ur.user_id = #{userId}
      """)
    Set<String> selectRoleNamesByUserId(@Param("userId") Long userId);

    @Select("""
      SELECT ur.role_id
        FROM user_roles ur
       WHERE ur.user_id = #{userId}
      """)
    Set<Long> selectRoleIdsByUserId(@Param("userId") Long userId);
}
